package com.example.pclimb.repository;

import android.app.Application;

import androidx.lifecycle.LiveData;

import com.example.pclimb.dao.ClimbRequestDao;
import com.example.pclimb.database.PclimbDatabase;
import com.example.pclimb.model.ClimbRequest;

import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 陪爬申请数据仓库，处理陪爬申请相关的所有数据操作
 */
public class ClimbRequestRepository {
    private ClimbRequestDao climbRequestDao;
    private LiveData<List<ClimbRequest>> pendingRequests;
    private LiveData<List<ClimbRequest>> matchedRequests;
    private LiveData<List<ClimbRequest>> awaitingPaymentRequests;
    private LiveData<List<ClimbRequest>> completedRequests;
    private LiveData<List<ClimbRequest>> cancelledRequests;
    private ExecutorService executorService;

    public ClimbRequestRepository(Application application) {
        PclimbDatabase database = PclimbDatabase.getInstance(application);
        climbRequestDao = database.climbRequestDao();
        pendingRequests = climbRequestDao.getPendingClimbRequests();
        matchedRequests = climbRequestDao.getMatchedClimbRequests();
        awaitingPaymentRequests = climbRequestDao.getAwaitingPaymentClimbRequests();
        completedRequests = climbRequestDao.getCompletedClimbRequests();
        cancelledRequests = climbRequestDao.getCancelledClimbRequests();
        executorService = Executors.newSingleThreadExecutor();
    }

    // 插入陪爬申请
    public void insert(ClimbRequest climbRequest, UserRepository.Callback<Long> callback) {
        executorService.execute(() -> {
            long id = climbRequestDao.insert(climbRequest);
            callback.onResult(id);
        });
    }

    // 更新陪爬申请
    public void update(ClimbRequest climbRequest) {
        executorService.execute(() -> {
            climbRequestDao.update(climbRequest);
        });
    }

    // 删除陪爬申请
    public void delete(ClimbRequest climbRequest) {
        executorService.execute(() -> {
            climbRequestDao.delete(climbRequest);
        });
    }

    // 获取待匹配的陪爬申请
    public LiveData<List<ClimbRequest>> getPendingRequests() {
        return pendingRequests;
    }

    // 获取已匹配的陪爬申请
    public LiveData<List<ClimbRequest>> getMatchedRequests() {
        return matchedRequests;
    }
    
    // 获取待支付的陪爬申请
    public LiveData<List<ClimbRequest>> getAwaitingPaymentRequests() {
        return awaitingPaymentRequests;
    }

    // 获取已完成的陪爬申请
    public LiveData<List<ClimbRequest>> getCompletedRequests() {
        return completedRequests;
    }
    
    // 获取已取消的陪爬申请
    public LiveData<List<ClimbRequest>> getCancelledRequests() {
        return cancelledRequests;
    }

    // 获取用户的所有陪爬申请
    public LiveData<List<ClimbRequest>> getUserRequests(int userId) {
        return climbRequestDao.getClimbRequestsByUserId(userId);
    }

    // 获取陪爬人员的所有陪爬申请
    public LiveData<List<ClimbRequest>> getGuideRequests(int guideId) {
        return climbRequestDao.getClimbRequestsByGuideId(guideId);
    }

    // 获取特定陪爬申请
    public LiveData<ClimbRequest> getClimbRequestById(int requestId) {
        return climbRequestDao.getClimbRequestById(requestId);
    }

    // 匹配陪爬申请
    public void matchRequest(int requestId, int guideId) {
        executorService.execute(() -> {
            climbRequestDao.matchRequest(requestId, guideId);
        });
    }
    
    // 设置为待支付状态
    public void setAwaitingPayment(int requestId) {
        executorService.execute(() -> {
            climbRequestDao.setAwaitingPayment(requestId);
        });
    }

    // 完成陪爬申请
    public void completeRequest(int requestId) {
        executorService.execute(() -> {
            climbRequestDao.completeRequest(requestId);
        });
    }

    // 取消陪爬申请
    public void cancelRequest(int requestId) {
        executorService.execute(() -> {
            climbRequestDao.cancelRequest(requestId);
        });
    }

    // 设置价格
    public void setPrice(int requestId, double price) {
        executorService.execute(() -> {
            climbRequestDao.setPrice(requestId, price);
        });
    }

    // 标记为已支付
    public void markAsPaid(int requestId) {
        executorService.execute(() -> {
            climbRequestDao.markAsPaid(requestId);
        });
    }

    // 按日期范围查询陪爬申请
    public LiveData<List<ClimbRequest>> getRequestsByDateRange(Date startDate, Date endDate) {
        return climbRequestDao.getClimbRequestsByDateRange(startDate, endDate);
    }

    // 按地点查询陪爬申请
    public LiveData<List<ClimbRequest>> getRequestsByLocation(String location) {
        return climbRequestDao.getClimbRequestsByLocation(location);
    }
} 